<?php
class Bind_user_model extends CI_Model
{
    private $_table = 'bind_user';

    public function insert($postData=array())
    {
        $data = array(
            'uid'         => $postData['uid'],
            'dy_uid'      => 0,
            'object_uid'  => '0',
            'open_id'     => empty($postData['openid']) ? 0 : $postData['openid'],
            'ali_user_id' => empty($postData['ali_user_id']) ? 0 : $postData['ali_user_id'],
            'created_at'  => date('Y-m-d H:i:s'),
            'updated_at'  => '00-00-00 00:00:00'
        );
        return $this->db->insert($this->_table, $data);
    }

    /**
     * 添加
     * @param $postData
     * @return mixed
     */
    public function insertBindUser($postData)
    {
        $data = array(
            'uid' => $postData['uid'],
            'dy_uid' => 0,
            'object_uid' => '0',
            'open_id' => empty($postData['openid']) ? 0 : $postData['openid'],
            'ali_user_id' => empty($postData['ali_user_id']) ? 0 : $postData['ali_user_id'],
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => '00-00-00 00:00:00'
        );
        return $this->db->insert($this->_table, $data);
    }

    /**
     * 根据微信帐号 查找
     * @param $openId
     * @return mixed
     */
    public function findByOpenId($openId)
    {
        $this->db->select('id, uid');
        $this->db->from($this->_table);
        $this->db->where('open_id', $openId);

        $this->db->limit(1);
        return $this->db->get();
    }

    public function find($params, $limit = 1)
    {
        if (is_object($params)) {
            $params = (array)$params;
        }

        if (empty($params['openid']) && empty($params['ali_user_id'])) {
            echo 'openid与ali_user_id必须只能传一个';exit;
        }

        $this->db->select('id, uid');
        $this->db->from($this->_table);
        if (!empty($params['openid'])) {
            $this->db->where('open_id', $params['openid']);
        }
        
        if (!empty($params['ali_user_id'])) {
            $this->db->where('ali_user_id', $params['ali_user_id']);
        }

        $this->db->limit($limit);
        return $this->db->get();
    }

    /**
     * 根据帐号UID 查找
     * @param $uid
     * @return mixed
     */
    public function findByUid($uid)
    {
        $this->db->select('*');
        $this->db->from($this->_table);
        $this->db->where('uid', $uid);
        $this->db->limit(1);
        return $this->db->get();
    }
    
    /**
     * 
     * @param unknown $uid
     * @param number $isWhere 1=>微信，2=>支付宝
     */
    public function findByOpenOrAli($uid, $isWhere=1)
    {
        $this->db->where('uid', $uid);
        if ($isWhere==1) {
            $this->db->where(array('open_id !='=>''));
        }
        if ($isWhere==2) {
            $this->db->where(array('ali_user_id !='=>''));
        }
        $this->db->limit(1);
        return $this->db->get($this->_table);
    }
    
    /**
     * 更新绑定信息
     * @param $postData
     * @return mixed
     */
    public function updateBindUser($postData)
    {
        $data = array(
            'uid' => $postData['uid'],
            'dy_uid' => 0,
            'object_uid' => '0',
            'open_id' => $postData['open_id']
        );
        $this->db->where('id', $postData['id']);
        return $this->db->update($this->_table, $data);
    }

    public function updateByOpenId($openId, $data)
    {
        $this->db->where('open_id', $openId);
        return $this->db->update($this->_table, $data);
    }

    /**
     * 更新绑定信息
     * @param $postData
     * @return mixed
     */
    public function update($postData=array())
    {
        $data = array(
            'uid'        => $postData['uid'],
            'dy_uid'     => 0,
            'object_uid' => '0',
            'open_id'    => $postData['open_id']
        );
        $this->db->where('id', $postData['id']);
        return $this->db->update($this->_table, $data);
    }
}